# api/v2/wcs/schemas.py
from datetime import datetime

from pydantic import BaseModel, Field
from typing import Optional, List

# WCS Location Schema
class LocationBase(BaseModel):
    """库位基础模型"""
    location: str = Field(..., examples=["1,1,1"], description="库位坐标 (格式: 'wh,x,y' - '库区,层,列')")
    status: int = Field(..., examples=[0,2,-1], description="库位状态 (0-空 1-叠放托盘 2-货物 -1-出入口)")
    pallet_id: Optional[str] = Field(..., examples=["T222_1"], description="托盘号 (任务号)")

class Location(LocationBase):
    """WCS库位模型"""
    id: int = Field(..., examples=[1], description="库位ID")
    update_time: datetime = Field(..., examples=["2025-09-29T03:40:02.126657"], description="库位更新时间 (格式为UTC时间)")

    class Config:
        from_attributes = True

# ========== requests models ==========
class GetLocationByLoc(BaseModel):
    """通过坐标获取库位信息模型"""
    location: str = Field(..., examples=["1,1,1"], description="库位坐标 (格式: 'wh,x,y' - '库区,层,列')")

class GetLocationByPalletID(BaseModel):
    """通过托盘号获取库位信息模型"""
    pallet_id: str = Field(..., examples=["T222_1"], description="托盘号 (任务号)")

class GetLocationByStatus(BaseModel):
    """通过库位状态获取库位信息模型"""
    status: int = Field(..., examples=[0], description="库位状态 (0-空 1-叠放托盘 2-货物 -1-出入口)")

class UpdateLocationByLoc(BaseModel):
    """通过坐标更新库位信息模型"""
    location: str = Field(..., examples=["1,1,3"], description="库位坐标 (格式: 'wh,x,y' - '库区,层,列')")
    pallet_id: str = Field(..., examples=["T111_2"], description="托盘号 (任务号)")

class DeleteLocationByLoc(BaseModel):
    """通过坐标删除取库位信息模型"""
    location: str = Field(..., examples=["1,1,3"], description="库位坐标 (格式: 'wh,x,y' - '库区,层,列')")


# Task Inband/Outband Schema
class TaskDescription(BaseModel):
    """定义入库任务详情"""
    task_type: int = Field(..., examples=[0], description="任务类型 (0-入库 1-出货)")
    gate_location: str = Field(..., examples=["1,1,1"], description="出入口坐标 (格式: 'wh,x,y' - '库区,层,列')")
    target_location: str = Field(..., examples=["1,4,4"], description="货物目标坐标 (格式: 'wh,x,y' - '库区,层,列')")

class Task(BaseModel):
    """定义单个入库任务的模型"""
    task_no: str = Field(..., examples=["T123_1"], description="任务号")
    description: List[TaskDescription] = Field(..., description="任务详情")

class TaskRequest(BaseModel):
    """定义整个入库任务请求体的模型"""
    stacker_location: str = Field(..., examples=["0,1,1"], description="堆垛机位置 (格式: '0,x,y' - '0,层,列')")
    tasks: List[Task] = Field(..., description="任务列表")

# Cargo Move Schema
class CarGoMoveTaskDescription(BaseModel):
    """定义入库任务详情"""
    task_type: int = Field(..., examples=[2], description="任务类型 (2-库内转移)")
    current_location: str = Field(..., examples=["2,3,1"], description="货物当前坐标 (格式: 'wh,x,y' - '库区,层,列')")
    target_location: str = Field(..., examples=["2,4,15"], description="货物目标坐标 (格式: 'wh,x,y' - '库区,层,列')")

class CarGoMoveTask(BaseModel):
    """定义单个入库任务的模型"""
    task_no: str = Field(..., examples=["T123_2"], description="任务号")
    description: List[CarGoMoveTaskDescription] = Field(..., description="任务详情")

class CarGoMoveRequest(BaseModel):
    """定义整个入库任务请求体的模型"""
    stacker_location: str = Field(..., examples=["0,3,14"], description="堆垛机位置 (格式: '0,x,y' - '0,层,列')")
    tasks: List[CarGoMoveTask] = Field(..., description="任务列表")


# ========== response models ==========
class ResponseLocationList(BaseModel):
    """单排库位信息响应体"""
    message: str = Field(..., examples=["数据接收成功"], description="响应消息")
    data: List[Location] = Field(..., description="库位信息列表")

class WarehouseList(BaseModel):
    """所有库排基础模型"""
    warehouse_1: List[Location] = Field(..., description="一排库位信息列表")
    warehouse_2: List[Location] = Field(..., description="二排库位信息列表")

class ResponseAllLocationList(BaseModel):
    """库位信息响应体"""
    message: str = Field(..., examples=["数据接收成功"], description="响应消息")
    data: List[WarehouseList] = Field(..., description="库位信息列表")

class WarehouseAndLocation(BaseModel):
    """所有库排基础模型"""
    location: Location = Field(..., description="库位信息列表")

class ResponseLocation(BaseModel):
    """单排库位信息响应体"""
    message: str = Field(..., examples=["数据接收成功"], description="响应消息")
    data: List[WarehouseAndLocation] = Field(..., description="库位信息列表")


class ResponseTask(BaseModel):
    """定义任务响应体"""
    task_no: str = Field(..., examples=["T222_1"], description="任务号")
    task_type: int = Field(..., examples=[0], description="任务类型 (0-入库 1-出货 2-库内转移)")
    path_1: List = Field(..., examples=[[1, "0,2,3", "1,1,1"]], description="路径1 - [动作，起点，终点]")
    path_2: List = Field(..., examples=[[2, "1,1,1", "2,1,3"]], description="路径2 - [动作，起点，终点]")
    end_location: str = Field(..., examples=["2,1,3"], description="最后位置")

class ResponseTaskList(BaseModel):
    """定义任务列表响应体"""
    message: str = Field(..., examples=["数据接收成功"], description="响应消息")
    total_tasks_received: int =  Field(..., examples=[1], description="接收的任务总数")
    data: List[ResponseTask] = Field(..., description="处理后的任务数据")


class ResponseMoveTask(BaseModel):
    """定义库内移动任务响应体"""
    task_no: str = Field(..., examples=["T222_1"], description="任务号")
    task_type: int = Field(..., examples=[2], description="任务类型 (2-库内转移)")
    path_1: List = Field(..., examples=[[1, "0,2,3", "2,1,3"]], description="路径1 - [动作，起点，终点]")
    path_2: List = Field(..., examples=[[2, "2,1,3", "1,4,4"]], description="路径2 - [动作，起点，终点]")
    end_location: str = Field(..., examples=["1,4,4"], description="最后位置")

class ResponseMoveTaskList(BaseModel):
    """定义库内移动任务列表响应体"""
    message: str = Field(..., examples=["数据接收成功"], description="响应消息")
    total_tasks_received: int =  Field(..., examples=[1], description="接收的任务总数")
    data: List[ResponseMoveTask] = Field(..., description="处理后的任务数据")